package com.dhcc.DRGsHosp.application.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.querychannel.JdbcTemplateObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DrgsIndexCalApplication;
import com.dhcc.DRGsHosp.application.MrqScoreApplication;
import com.dhcc.DRGsHosp.core.domain.MrqScore;

/**
 *
 * @Description:    病例质控得分
 * @param:
 * @return:
 * @auther: chenhanlin
 * @date: 2018/7/26 16:15
 */
@Named
@Transactional
public class MrqScoreApplicationImpl implements MrqScoreApplication {
	
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
    @Autowired
    private NamedParameterJdbcTemplate jdbcTempSeverice;
    @Autowired
    private DrgsIndexCalApplication drgsIndexCalApplication ;

	/**
	 * 删除质控病历
	 * @auther: qiaoyilong
	 * @date: 2018/12/28 16:15
	 */
	@Override
	public void removeMrqScoreByPmainIndex(String pmainIndex) {
		if( pmainIndex != null && !"".equals(pmainIndex)){
			String sql = new String("delete from drgs.MRQ_SCORE_BASEINFO score where score.PMAININDEX = '"+pmainIndex+"' ");
			jdbcTemplateObj.update(sql);
			sql = new String("delete from drgs.MRQ_SCORE_TOTAL score where score.PMAININDEX = '"+pmainIndex+"' ");
			jdbcTemplateObj.update(sql);
		}
	}
	/**
	 * 保存质控病历大类得分
	 * @auther: qiaoyilong
	 * @date: 2018/12/28 16:15
	 */
	@Override
	public void saveMrqScore(MrqScore mrqScore) {
        if(null != mrqScore){
            List<MrqScore> mrqScoreTotalList  = new ArrayList<>() ;
            Map<String,Double> checkScoreMap = mrqScore.getCheckScoreMap();
            if(null != checkScoreMap && checkScoreMap.size()>0){
                Iterator csMap = checkScoreMap.entrySet().iterator();
                while (csMap.hasNext()) {
                	Map.Entry entry = (Map.Entry) csMap.next();
                	String key = (String)entry.getKey();
                	Double score = (Double)entry.getValue();
                    String[] keyList = key.split("@@");
                    if( null != keyList && keyList.length>1){
                        MrqScore totalScore = new MrqScore();
                        totalScore.setPmainIndex(mrqScore.getPmainIndex());
                        totalScore.setScoreCode(keyList[0]);
                        totalScore.setWeightCode(keyList[1]);
                        totalScore.setScore(score);
                        totalScore.setId(mrqScore.getPmainIndex()+"-"+keyList[0]+"-"+keyList[1]);
                        mrqScoreTotalList.add(totalScore);
                    }
                }
            }
            String sqlInsertTotalScore = "INSERT  INTO drgs.MRQ_SCORE_TOTAL (ID,PMAININDEX,SCORE_CODE,WEIGHT_CODE,SCORE)  VALUES (:id,:pmainIndex,:scoreCode,:weightCode,:score)" ;       
            drgsIndexCalApplication.batchUpdate(sqlInsertTotalScore,mrqScoreTotalList) ;
            
            String sqlInsertBaseInfoScore = "INSERT  INTO drgs.MRQ_SCORE_BASEINFO (PMAININDEX,ORG_CODE,DEP_CODE,RATING_LEVEL,CHECK_DATE,OUT_DATE,COMPLETE_FLAG,TOTAL_SCORE)  VALUES (:pmainIndex,:orgCode,:depCode,:ratingLevel,:checkDate,:outDate,:completeFlag,:totalScore)" ;
    		Map<String,Object> paramMap = new HashMap<String,Object>();
    		paramMap.put("pmainIndex", mrqScore.getPmainIndex());
    		paramMap.put("orgCode", mrqScore.getOrgCode());
    		paramMap.put("depCode", mrqScore.getDepCode());
    		paramMap.put("ratingLevel", mrqScore.getRatingLevel());
    		paramMap.put("checkDate", mrqScore.getCheckDate());
    		paramMap.put("outDate", mrqScore.getOutDate());
    		paramMap.put("completeFlag", mrqScore.getCompleteFlag());
    		paramMap.put("totalScore", mrqScore.getTotalScore());
    		int count = jdbcTempSeverice.update(sqlInsertBaseInfoScore, paramMap);        	
        }
	}

	/**
	 *  功能描述： 取得分类名称
	 *  @Date:2018年12月31日下午8:21:02
	 */
	@Override	
	public List<String> getScoreCode() {
		List<String> list = new ArrayList<String>();
		String sql = "select distinct t.score_code from drgs.MRQ_SCORE_TOTAL t ";
		list = jdbcTemplateObj.queryForList(sql, String.class);		
		return list;
	}

//	@Override
//	public MrqScore initializeFullScore() {
//		StringBuffer strb = new StringBuffer() ;
//		strb.append(" select * from ( \n ") ;
//		strb.append(" select weight.SCORE_CODE||'-'||weight.WEIGHT_CODE ccode ,weight.WEIGHT_SCORE score from drgs.MRQ_DICT_WEIGHT weight \n ") ;
//		strb.append(" union \n ") ;
//		strb.append(" select score.SCORE_CODE,score.SCORE_SCORE from drgs.MRQ_DICT_SCORE score \n ") ;
//        strb.append(" ) pivot (sum(score) for ccode in ('BASICINFO' BASICINFO, 'BASICINFO-A' BASICINFO_A, 'BASICINFO-B' BASICINFO_B,\n" );
//        strb.append("'BASICINFO-C' BASICINFO_C,'BASICINFO-D' BASICINFO_D,'COST' COST,'COST-A' COST_A,'COST-D' COST_D,'DT' DT,'DT-A' DT_A,'DT-B' DT_B,'DT-C' DT_C,'DT-D' DT_D,'IP' IP,'IP-A' IP_A,'IP-B' IP_B,'IP-C' IP_C,'IP-D' IP_D)) \n ") ;
//        Map<String,Object> map = jdbcTemplateObj.queryForMap(strb.toString()) ;
//        MrqScore score = new MrqScore() ;
//        if(map!=null){
//			score.setBasicinfoScore(MonitorResultUtil.objectToDouble(map.get("BASICINFO")));
//			score.setBasicinfoAScore(MonitorResultUtil.objectToDouble(map.get("BASICINFO_A")));
//			score.setBasicinfoBScore(MonitorResultUtil.objectToDouble(map.get("BASICINFO_B")));
//			score.setBasicinfoCScore(MonitorResultUtil.objectToDouble(map.get("BASICINFO_C")));
//			score.setBasicinfoDScore(MonitorResultUtil.objectToDouble(map.get("BASICINFO_D")));
//
//			score.setIpScore(MonitorResultUtil.objectToDouble(map.get("IP")));
//			score.setIpAScore(MonitorResultUtil.objectToDouble(map.get("IP_A")));
//			score.setIpBScore(MonitorResultUtil.objectToDouble(map.get("IP_B")));
//			score.setIpCScore(MonitorResultUtil.objectToDouble(map.get("IP_C")));
//			score.setIpDScore(MonitorResultUtil.objectToDouble(map.get("IP_D")));
//
//			score.setDtScore(MonitorResultUtil.objectToDouble(map.get("DT")));
//			score.setDtAScore(MonitorResultUtil.objectToDouble(map.get("DT_A")));
//			score.setDtBScore(MonitorResultUtil.objectToDouble(map.get("DT_B")));
//			score.setDtCScore(MonitorResultUtil.objectToDouble(map.get("DT_C")));
//			score.setDtDScore(MonitorResultUtil.objectToDouble(map.get("DT_D")));
//
//			score.setCostScore(MonitorResultUtil.objectToDouble(map.get("COST")));
//			score.setCostAScore(MonitorResultUtil.objectToDouble(map.get("COST_A")));
//			score.setCostBScore(MonitorResultUtil.objectToDouble(map.get("COST_B")));
//			score.setCostCScore(MonitorResultUtil.objectToDouble(map.get("COST_C")));
//			score.setCostDScore(MonitorResultUtil.objectToDouble(map.get("COST_D")));
//		}
//		return score;
//	}
}
